//
//  XQSettingController.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit

class XQSettingController: XQBaseController {
    
    private lazy var bgImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "setting_bg")
        imageView.isUserInteractionEnabled = true
        return imageView
    }()

    private lazy var logoImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "AppIcon")
        imageView.layer.cornerRadius = 23
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    private lazy var versionLb : UILabel = {
       let lb = UILabel()
        lb.text = "V\(XQDeviceManager.appVersion())"
        lb.textColor = .xq_black
        lb.font = UIFont.boldSystemFont(ofSize: 16)
        return lb
    }()
    
    private lazy var logOutBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Log out", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        btn.backgroundColor = .mainTextColor
        btn.layer.cornerRadius = 24
        btn.addTarget(self, action: #selector(logOutClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var cancelAccountBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Account cancellation", for: .normal)
        btn.setTitleColor(.mainTextColor, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        btn.backgroundColor = .clear
        btn.layer.cornerRadius = 24
        btn.layer.borderWidth = 1.0
        btn.layer.borderColor = UIColor.mainTextColor.cgColor
        btn.addTarget(self, action: #selector(cancelClick), for: .touchUpInside)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        makeNav()
        makeUI()
        makeConstraints()
    }
    
    private func makeNav() {
        self.navTitle = "Settings"
        self.navTitleColor = .white
    }
    
    private func makeUI() {
        self.view.insertSubview(self.bgImageView, belowSubview: self.navigationBar)
        self.bgImageView.addSubview(self.logoImageView)
        self.bgImageView.addSubview(self.versionLb)
        self.bgImageView.addSubview(self.logOutBtn)
        self.bgImageView.addSubview(self.cancelAccountBtn)
    }
    
    private func makeConstraints() {
        self.bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        self.logoImageView.snp.makeConstraints { make in
            make.top.equalTo(UIDevice.xq_statusHeight() + 78)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(120)
        }
        self.versionLb.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.logoImageView.snp.bottom).offset(31)
        }
        self.logOutBtn.snp.makeConstraints { make in
            make.top.equalTo(self.versionLb.snp.bottom).offset(28)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(48)
        }
        self.cancelAccountBtn.snp.makeConstraints { make in
            make.top.equalTo(self.logOutBtn.snp.bottom).offset(15)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(48)
        }
    }
    
    @objc private func logOutClick() {
        let contentView = XQLogoutView()
        contentView.frame = CGRect(x: 0, y:( UIDevice.xq_height() - 396) / 2, width: UIDevice.xq_width(), height: 396)
        contentView.sureHandle = {[weak self] in
            guard let srongify = self else {return}
            srongify.logout()
            XQHud.hidePop()
        }
        contentView.cancelHandle = {
            XQHud.hidePop()
        }
        XQHud.showPop(contentView)
    }
    
    private func logout() {
        XQHud.showLoading(controller: self)
        XQNetworkManager.shared.getRequest(url: "/figures/private") {[weak self] response, error in
            XQHud.hideLoading()
            guard let srongify = self else {return}
            
            XQCache.saveSessionId(sessionId: "")
            XQNetworkManager.shared.setSessionId(sessionId: "")

            srongify.navigationController?.popViewController(animated: true)
            
            XQRouteTool.jumpHome()
        }
    }
    
    @objc private func cancelClick() {
        let vc = XQAccountCancelController()        
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}
